set more off

use dataset, clear

*** covariates ***

local X1 totalrevownsourcespcp pcttotaltax pcttotalig
local X2 lpopulation popdensity incomepcp pctba pct65plus pcthispanic pctforeign herf H
local X3 crimepcp totalpolicepcp dem ngovspcp netmigration
 
replace pctblack = log(pctblack + 1)

local X `X1' `X2' `X3' `X4'

foreach x in `X' pctblack {
	su `x'
	replace `x' = (`x' - r(min)) / (r(max) - r(min))
	drop if mi(`x')
}


eststo clear

eststo: reg lpctfinesand pctblack, robust
preserve
	replace pctblack = 1
	predict yhat, xb
	su yhat
restore
local y1 = r(mean)
preserve
	replace pctblack = 0
	predict yhat, xb
	su yhat
restore
local y0 = r(mean)
estadd scalar mydiff = exp(`y1')-exp(`y0')


eststo: reg lpctfinesand pctblack `X1', robust
preserve
	replace pctblack = 1
	predict yhat, xb
	su yhat
restore
local y1 = r(mean)
preserve
	replace pctblack = 0
	predict yhat, xb
	su yhat
restore
local y0 = r(mean)
estadd scalar mydiff = exp(`y1')-exp(`y0')
estadd local controlsfinances = "\checkmark"

eststo: reg lpctfinesand pctblack `X1' `X2', robust
preserve
	replace pctblack = 1
	predict yhat, xb
	su yhat
restore
local y1 = r(mean)
preserve
	replace pctblack = 0
	predict yhat, xb
	su yhat
restore
local y0 = r(mean)
estadd scalar mydiff = exp(`y1')-exp(`y0')
estadd local controlsfinances = "\checkmark"
estadd local controlsdemographics = "\checkmark"

eststo: reg lpctfinesand pctblack `X1' `X2' `X3', robust
preserve
	replace pctblack = 1
	predict yhat, xb
	su yhat
restore
local y1 = r(mean)
preserve
	replace pctblack = 0
	predict yhat, xb
	su yhat
restore
local y0 = r(mean)
estadd scalar mydiff = exp(`y1')-exp(`y0')
estadd local controlsfinances = "\checkmark"
estadd local controlsdemographics = "\checkmark"
estadd local controlscounty = "\checkmark"
 
 
 
 
label variable pctblack "Percent black population"

label variable lH "Log segregation"

label variable herf "Herfindahl index"

#delimit;

esttab 
	using "tableA5.tex"
	,
		replace 
		label
		booktabs
		cells(b(fmt(2) star) se(par))
		collabels(none)
		nomtitles
		width(\textwidth)
		stats(
			N r2
			,
				label("Sample size" "R-squared")
				fmt(%20.0fc %20.02fc)
		)		
		compress
		;
#delimit cr


esttab, keep(pctblack) t stats(mydiff mydiffse N r2)
